package com.example.syssoft.service;

import com.example.syssoft.common.service.LogicService;
import com.example.syssoft.config.Result;
import com.example.syssoft.dao.UserDao;
import com.example.syssoft.domain.User;
import com.example.syssoft.security.JwtHelp;
import com.example.syssoft.utils.CookiesUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Service
public class UserService extends LogicService<UserDao, User,Long> {

    @Resource
    UserDao userdao;

    private final JwtHelp jwtHelp;

    public UserService(UserDao lr,JwtHelp jwtHelp) {
        super(lr);
       this.jwtHelp = jwtHelp;
    }
    public Result login(HttpServletResponse response, String username, String password) {
        User user = userdao.findUserByUsername(username);
        if(user.getPassword().equals(password)){
            String token = jwtHelp.generateToken(user);
            // 这里不一定是采用cookie，如果是移动端，可以直接返回
            CookiesUtils.setCookies(response, "token", token);
            return new Result(true,user,"登录成功！");
//            return user;
        }else
        {
            // 采用异常流处理业务逻辑
            return new Result(false,"用户名或密码错误");
        }
    }



}
